package org.br.erp.rfq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.br.erp.base.utils.ERPUtils;
import org.br.erp.rfq.entity.Scminquiryitem;
import org.br.erp.rfq.entity.Scminquiryitemlinkvendor;
import org.br.erp.rfq.mapper.ScminquiryitemMapper;
import org.br.erp.rfq.mapper.ScminquiryitemlinkvendorMapper;
import org.br.erp.rfq.service.IScminquiryitemlinkvendorService;
import org.br.erp.rfq.vo.ScminquiryitemLinkVendorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Description: 供应商查看询价单信息
 * @Author: jeecg-boot
 * @Date:   2023-01-14
 * @Version: V1.0
 */
@Service
public class ScminquiryitemlinkvendorServiceImpl extends ServiceImpl<ScminquiryitemlinkvendorMapper, Scminquiryitemlinkvendor> implements IScminquiryitemlinkvendorService {

    @Autowired
    private ScminquiryitemlinkvendorMapper scminquiryitemlinkvendorMapper;

    @Autowired
    private ScminquiryitemMapper scminquiryitemMapper;

    @Override
    public List<ScminquiryitemLinkVendorVo> queryLinkVendorInfo(IPage<ScminquiryitemLinkVendorVo> page, Map paramMap) {

        QueryWrapper<ScminquiryitemLinkVendorVo> queryWrapper=new QueryWrapper<>();
        if(ERPUtils.ifHtppReqParamContainKey(paramMap,"companyid")){
            queryWrapper.eq("head.companyid",ERPUtils.getHttpReqParam(paramMap,"companyid"));
        }
        if(ERPUtils.ifHtppReqParamContainKey(paramMap,"vendorid")){
            //根据所属的供应商信息只显示对应供应商报价信息
            queryWrapper.eq("vendoritem.vendorid",ERPUtils.getHttpReqParam(paramMap,"vendorid"));
        }
        if(ERPUtils.ifHtppReqParamContainKey(paramMap,"vendorconfirmflag")){
            queryWrapper.eq("vendoritem.vendorconfirmflag",ERPUtils.getHttpReqParam(paramMap,"vendorconfirmflag"));
        }
        List<ScminquiryitemLinkVendorVo> scminquiryitemLinkVendorVos = scminquiryitemlinkvendorMapper.queryLinkVendorInfo(page, queryWrapper);
        String nowDate = ERPUtils.getNowDate();
        for (ScminquiryitemLinkVendorVo scminquiryitemLinkVendorVo : scminquiryitemLinkVendorVos) {
            String validdate = scminquiryitemLinkVendorVo.getValiddate();
            if(validdate == null){
                continue;
            }
           //当前日期大于询价截止日期，则为不可询价状态
            if(nowDate.compareTo(validdate) >=0){
               scminquiryitemLinkVendorVo.setIsvalid("0");
           }else{
               scminquiryitemLinkVendorVo.setIsvalid("1");
           }
        }

        return scminquiryitemLinkVendorVos;
    }

    /**
     * 供应商确认数量和价格
     * @param linkvendorid
     * @param vendorqty
     * @param vendorprice
     */
    @Override
    @Transactional
    public void confirmInquiry(String linkvendorid, String vendorqty, String vendorprice) {
        Scminquiryitemlinkvendor scminquiryitemlinkvendor=scminquiryitemlinkvendorMapper.selectById(linkvendorid);
        scminquiryitemlinkvendor.setConfirmdate(ERPUtils.getNowDate());
        scminquiryitemlinkvendor.setVendorqty(vendorqty);
        scminquiryitemlinkvendor.setVendorprice(vendorprice);
        scminquiryitemlinkvendor.setVendorconfirmflag("1");
        scminquiryitemlinkvendorMapper.updateById(scminquiryitemlinkvendor);


        //再去更新当前分录的报价标识
        String inquiryitemid = scminquiryitemlinkvendor.getInquiryitemid();

        Scminquiryitem scminquiryitem = scminquiryitemMapper.selectById(inquiryitemid);

        if(scminquiryitem==null){
            return;
        }

        QueryWrapper<Scminquiryitemlinkvendor> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("inquiryitemid",inquiryitemid);

        List<Scminquiryitemlinkvendor> scminquiryitemlinkvendors = scminquiryitemlinkvendorMapper.selectList(queryWrapper);
        //全部供应商询的信息
        int size = scminquiryitemlinkvendors.size();

        //再判断当前供应商已确认的信息
        queryWrapper.eq("vendorconfirmflag","1");
        List<Scminquiryitemlinkvendor> confirmlinkvendor = scminquiryitemlinkvendorMapper.selectList(queryWrapper);
        int confirmsize = confirmlinkvendor.size();
        if(confirmsize ==   size){
            scminquiryitem.setQuoteFlag("2");
            scminquiryitem.setQuoteNum(size);
        }
        else if(confirmsize == 0){
            scminquiryitem.setQuoteFlag("0");
            scminquiryitem.setQuoteNum(0);
        } else if(confirmsize < size){
            scminquiryitem.setQuoteFlag("1");
            scminquiryitem.setQuoteNum(confirmsize);
        }
        scminquiryitemMapper.updateById(scminquiryitem);
    }

}
